package com.small.xx.ai.apidoc.controller;

import com.small.xx.ai.apidoc.dto.AppServiceRequest;
import com.small.xx.ai.apidoc.dto.AppServiceResponse;
import com.small.xx.ai.apidoc.service.AppServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 应用服务管理控制器
 */
@Controller
@RequestMapping("/api-doc/app-service")
public class AppServiceController {
    
    @Autowired
    private AppServiceService appServiceService;
    
    /**
     * 显示应用服务列表页面
     * 
     * @param page 页码
     * @param size 每页大小
     * @param model 模型对象
     * @return 应用服务列表模板
     */
    @GetMapping("/list")
    public String listAppServices(@RequestParam(defaultValue = "1", name = "age") int page,
                                  @RequestParam(defaultValue = "10", name = "size") int size,
                                  Model model) {
        List<AppServiceResponse> appServices = appServiceService.getAppServicesByPage(page, size);
        int total = appServiceService.getAppServiceCount();
        int totalPages = (int) Math.ceil((double) total / size);
        
        model.addAttribute("appServices", appServices);
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", totalPages);
        model.addAttribute("pageSize", size);
        model.addAttribute("total", total);
        return "api-doc/app-service/list";
    }
    
    /**
     * 显示新增应用服务页面
     * 
     * @return 新增应用服务模板
     */
    @GetMapping("/create")
    public String createAppServicePage() {
        return "api-doc/app-service/create";
    }
    
    /**
     * 处理新增应用服务请求
     * 
     * @param request 应用服务请求
     * @param model 模型对象
     * @return 重定向到应用服务列表页面
     */
    @PostMapping("/create")
    public String createAppService(@ModelAttribute AppServiceRequest request, Model model) {
        try {
            AppServiceResponse response = appServiceService.createAppService(request);
            return "redirect:/api-doc/app-service/list";
        } catch (Exception e) {
            model.addAttribute("error", "创建应用服务失败: " + e.getMessage());
            return "api-doc/app-service/create";
        }
    }
    
    /**
     * 显示编辑应用服务页面
     * 
     * @param id 应用服务ID
     * @param model 模型对象
     * @return 编辑应用服务模板
     */
    @GetMapping("/edit/{id}")
    public String editAppServicePage(@PathVariable(name = "id") Long id, Model model) {
        AppServiceResponse appService = appServiceService.getAppServiceById(id);
        if (appService == null) {
            model.addAttribute("error", "应用服务不存在");
            return "redirect:/api-doc/app-service/list";
        }
        model.addAttribute("appService", appService);
        return "api-doc/app-service/edit";
    }
    
    /**
     * 处理编辑应用服务请求
     * 
     * @param id 应用服务ID
     * @param request 应用服务请求
     * @param model 模型对象
     * @return 重定向到应用服务列表页面
     */
    @PostMapping("/edit/{id}")
    public String editAppService(@PathVariable(name = "id") Long id, @ModelAttribute AppServiceRequest request, Model model) {
        try {
            AppServiceResponse response = appServiceService.updateAppService(id, request);
            if (response == null) {
                model.addAttribute("error", "应用服务不存在");
                return "api-doc/app-service/edit";
            }
            return "redirect:/api-doc/app-service/list";
        } catch (Exception e) {
            model.addAttribute("error", "更新应用服务失败: " + e.getMessage());
            return "api-doc/app-service/edit";
        }
    }
    
    /**
     * 处理删除应用服务请求
     * 
     * @param id 应用服务ID
     * @return 重定向到应用服务列表页面
     */
    @PostMapping("/delete/{id}")
    public String deleteAppService(@PathVariable(name = "id") Long id, Model model) {
        try {
            boolean deleted = appServiceService.deleteAppService(id);
            if (!deleted) {
                model.addAttribute("error", "应用服务不存在");
            }
            return "redirect:/api-doc/app-service/list";
        } catch (Exception e) {
            model.addAttribute("error", "删除应用服务失败: " + e.getMessage());
            return "redirect:/api-doc/app-service/list";
        }
    }
    
    /**
     * 显示应用服务详情页面
     * 
     * @param id 应用服务ID
     * @param model 模型对象
     * @return 应用服务详情模板
     */
    @GetMapping("/detail/{id}")
    public String appServiceDetail(@PathVariable(name = "id") Long id, Model model) {
        AppServiceResponse appService = appServiceService.getAppServiceById(id);
        if (appService == null) {
            model.addAttribute("error", "应用服务不存在");
            return "redirect:/api-doc/app-service/list";
        }
        model.addAttribute("appService", appService);
        return "api-doc/app-service/detail";
    }
}